<template>
  <view class="my-settle-container">
    <label class="radio" @click="CheckedAll">
      <radio color="#C00000" :checked="isAllChecked" /><text>全选</text>
    </label>
    <view class="total-price">
      合计:<text>￥{{checkedTotalCount}}</text>
    </view>
    <view class="btn-settle" @click="settleClickHandel">
      结算({{checkedCount}})
    </view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex'
  export default {
    name:"my-settle",
    data() {
      return {
        //3秒后自动跳转登录页
        second:3,
        timeID:null
        
      };
    },
    computed:{
      ...mapGetters('m_cart',['checkedCount','total','checkedTotalCount']),
      ...mapGetters('m_address',['addStr']),
      ...mapState('m_address',['token','redirectInfo']),
      ...mapState('m_cart',['cart']),
      isAllChecked(){
       return this.checkedCount === this.total
      }
    },
    methods:{
      ...mapMutations('m_cart',['allChecked']),
      ...mapMutations('m_address',['updateRedirectInfo']),
      CheckedAll(){
        this.allChecked(!this.isAllChecked)
      },
      //点击结算
      settleClickHandel(){
        // 判断是否勾选了商品
        if(this.checkedCount === 0) return uni.$showMsg('请勾选商品')
        //判断是否选择了收货地址
        if(!this.addStr) return uni.$showMsg('请选择收货地址')
        //判断是否登录
        // if(!this.token) return uni.$showMsg('请先登录')
        if(!this.token) return this.delayNavigete()
        //创建订单
        this.payOrder()
        
      },
     async payOrder(){
        //  创建订单
        // 组织订单的信息对象
        const orderInfo = {
          order_price:0.01,
          consignee_addr:this.addStr,
          //这边注意map的返回值，对象外面要包()
          goods:this.cart.filter(x=>x.goods_state).map(item=>({
            goods_id:item.goods_id,
            goods_number:item.goods_count,
            goods_price:item.goods_price
          }))
        }
        //请求接口，把组织订单的信息对象传进去，用于获得订单编号
        const res = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        // console.log(res)
        if(res.data.meta.status !== 200) return uni.$showMsg('创建订单失败')
        const orderNumber = res.data.message.order_number
        
        //请求接口，预支付，把订单编号传进去
        const res2 = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
         if(res2.data.meta.status !== 200) return uni.$showMsg('订单预支付失败')
         // 得到订单支付相关的必要参数
         //此处 payInfo 获取失败，返回不了正确的数据,导致后面的 uni.requestPayment 无法发起
         const payInfo = res2.data.message.pay
         console.log(payInfo)
         
         //发起微信支付
         //无法获取参数
        // uni.requestPayment({
        //  provider:'wxpay',
        //  orderInfo,
        //  timeStamp:Date.now(),
        //  nonceStr:orderInfo.nonce_str,
        //  package:data.package,
        //  signType:data.signType,
        //  paySign:data.paySign,
        //  success: (result) => {
        //    console.log(result)
        //  },
        //  fail: (error) => {
        //    console.log(error)
        //  }
          
        // })
       
      },
     //显示toast
      showTips(n){
        uni.showToast({
          title:'请登录后再结算,'+n+'秒后自动跳转登录',
          icon:'none',
          mask:true,
          duration: 1500
        })
      },
       //3秒自动跳转的方法
      delayNavigete(){
        console.log('11',this.second)
        this.second = 3
        this.showTips(this.second)
        this.timeID=  setInterval(()=>{
           this.second--
          if(this.second <= 0){
            clearInterval(this.timeID)
            uni.switchTab({
              url:'/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  openType:'switchTab',
                  from:'page/cart/cart'
                })
              }
            })
            return 
          }
          this.showTips(this.second)
        },1000)
      }
    }
  }
</script>

<style lang="scss">
.my-settle-container{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  .radio{
    display: flex;
    align-items: center;
    margin-left: 5px;
  }
  .total-price{
    text{
      color: #C00000;
    }
  }
  .btn-settle{
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-color: #C00000;
    color: #fff;
    min-width: 100px;
  }
}
</style>